package com.zhongli.devplatform.bean;


import lombok.Data;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Data
public class Res<T> {

    private int code;
    private String msg;
    private Date timestamp = new Date();
    private T data;

    public static int SERVICE_ERROR_CODE = 208;

    public Res() {
    }

    public Res(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Res(T data) {
        this.code = HttpServletResponse.SC_OK;
        this.data = data;
    }

    public Res(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static<T> Res<T> ok(T data){
        Res<T> Res = new Res<T>(data);
        Res.setCode(0);//操作成功
        Res.setMsg("成功");
        return Res;
    }

    public static<T> Res<T> not_ok(String msg){
        Res<T> Res = new Res<T>();
        Res.setCode(1);//操作失败
        Res.setMsg(msg);
        return Res;
    }

    public Res<T> msg(String msg){
        this.setMsg(msg);
        return this;
    }

    public static<T> Res<T> fail(T data){
        Res<T> Res = new Res<T>();
        Res.setCode(1);//操作失败
        Res.setMsg((String) data);
        return Res;
    }

    public static Res error(String msg) {
        return new Res(SERVICE_ERROR_CODE, msg);
    }


}
